import React, { Component } from 'react';
import { Form, Input, Select ,message } from 'antd';
import Model from '@/models/user'
const layout = {
    labelCol: {
      span: 8,
    },
    wrapperCol: {
      span: 16,
    },
  };
class AddUser extends Component {
  state = {
    alldata:[]
  }
    render() {
      let {sentrole} =this.props
        return (
            <>
            <Form
                {...layout}
                name="adduser"
                initialValues={{
                  remember: true,
                }}
                onFieldsChange={this.getallfields}
              >
                <Form.Item
                    name="username"
                    label="用户名"
                    tooltip="请输入你的昵称"
                    rules={[
                        {
                            required: true,
                            message: '请输入你的昵称',
                            whitespace: true,
                        }
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    name="password"
                    label="密码"
                    tooltip="给爷填"
                    rules={[
                        {
                            required: true,
                            message: '请输入你的密码',
                            whitespace: true,
                        },
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    name="phone"
                    label="手机号"
                    tooltip="手机号11位"
                    rules={[
                        {
                            required: false,
                            message: '请输入你的手机号',
                            whitespace: true,
                        }
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    name="email"
                    label="邮箱"
                    tooltip="请输入你的邮箱！"
                    rules={[
                        {
                            required: false,
                            message: '请输入你的密码',
                            whitespace: true,
                        }
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    name="role_id"
                    label="角色"
                    rules={[
                      {
                        required: false,
                        message: '请选择角色！',
                      },
                    ]}
                  >
                    <Select placeholder="请选择角色" >
                      {
                          sentrole.map(el=>{
                          return (
                            <Select.Option key={el._id}value={el._id}>{el.name}</Select.Option>
                          )
                        })
                      }
                    </Select>
                  </Form.Item>
                </Form>
            </>
        );
    }
    componentDidUpdate () {
      if(this.props.submitadd)
      {
        let {alldata} = this.state
        // console.log(this.state.alldata);
        let values = {}
        values['username'] = alldata[0]?alldata[0]:''
        values['password'] = alldata[1]?alldata[1]:''
        values['phone'] = alldata[2]?alldata[2]:''
        values['email'] = alldata[3]?alldata[3]:''
        values['role_id'] = alldata[4]?alldata[4]:''
        // console.log(values);
        // console.log('提交表单信息');
          // let a = parseInt((Math.random()+1)*1000)
          // values['username'] = alldata[0]+a
        Model.addUser(values).then(ret=>{
        //   console.log(ret);
          if(ret.status===0)
          message.success("创建成功！")
          else
          message.error(ret.msg)
        })
      
        return true
      }
      else  
      return false
    }
    getallfields=(c,all)=>{
      // console.log(all.length);
      let alldata = []
    for(var i=0 ;i<all.length;i++)
          alldata.push(all[i].value)
      // console.log(alldata);
     this.setState({
        alldata
     })
    }
}

export default AddUser;
